package com.oocl.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.oocl.db.FoodTypeDB;
import com.oocl.factory.ServiceFactory;
import com.oocl.log4j.Log4j;
import com.oocl.pojo.Food;
import com.oocl.pojo.FoodType;
import com.oocl.pojo.Merchant;
import com.oocl.service.FoodService;
import com.oocl.service.MerchantService;
import com.oocl.service.impl.FoodServiceImpl;
import com.oocl.service.impl.MerchantServiceImpl;
import com.oocl.util.ObjConvertUtil;
import com.oocl.vo.MerchandiseVO;

/**
 * Servlet implementation class ShowMenuServlet
 */
public class ShowFoodByUnameServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
    private FoodService foodService;   
    private MerchantService merchantService;

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		foodService = ServiceFactory.getFoodService();
		merchantService = ServiceFactory.getMerchantService();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * 根据 商家的 Id，加载 其 所有 的 食品信息
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String uname = request.getParameter("uname");
		HttpSession session = request.getSession();
		Merchant merchant = merchantService.findByName(uname);
		Log4j.getLogger().info("根据名字从数据库查找出来的商家信息为 {} ", merchant.toString());
		session.setAttribute("merchant", merchant);
		//加载 该商家下的所有食品 信息
				
		List<Food> foods = foodService.getFoodByMerchantId(merchant.getId());
		List<MerchandiseVO> merchandiseVOs =ObjConvertUtil.foodDOsToMerchaniseVOs(foods);
		
		//测试： 转换后的 merchandiseVOs 为： 
		for(MerchandiseVO m:merchandiseVOs){
			System.out.println(m.toString());
		}
		
		
		request.setAttribute("merchandiseVOs", merchandiseVOs);
		session.setAttribute("foodTypes", FoodTypeDB.getFoodTypes());
		request.getRequestDispatcher("view/showFoodByUname.jsp").forward(request, response);
		
	}

}
